"use client";
import type { Root } from "react-dom/client";
import { isClient } from "@/app/utils";
import Loading from "@/app/components/Loading";

let holderFragment: any = null;
let Loadings: any[] = [];
let root: Root;
function createContainer() {
  holderFragment = document.createElement("div");
  document.body.appendChild(holderFragment);
}
function unmount() {
  root?.unmount();
  document.body.removeChild(holderFragment);
  holderFragment = null;
}
function open() {
  if (!isClient()) return;
  if (!holderFragment) {
    createContainer();
  }
  const key = Date.now();
  Loadings = [<Loading key={key} />];

  root.render(Loading);
}

let createLoading = { open, close: unmount };

export default createLoading;
